<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapperPkg}.${mapperClassName}">
    <!-- 基本实体映射 -->
    <resultMap id="${entityClassName}Result" type="${entityPkg}.${entityClassName}">
    <#list columns as field>
        <#-- 主键 -->
        <#if field.isPk == "1" >
        <id column="${field.columnName}" property="${field.javaField}"/>
        <#-- 普通字段 -->
        <#else>
        <result column="${field.columnName}" property="${field.javaField}"/>
        </#if>
    </#list>
    </resultMap>

    <!-- 通用查询 -->
    <sql id="select${entityClassName}">
        SELECT <#list columns as field>`${field.columnName}`<#if !field?is_last>, </#if></#list>
        FROM ${tableName}
    </sql>


    <!--列表查询-->
    <select id="select${entityClassName}List" parameterType="${dtoPkg}.${dtoListClassName}"
            resultMap="${entityClassName}Result">
        <include refid="select${entityClassName}"/>
        WHERE del_flag = 'F'
<#list columns as field>
    <#if field.isQuery== "1">
    <#switch field.queryType>
        <#case "BETWEEN">
            <#-- 范围查询 -->
            <if test="${field.javaField + 'Start'} != null and ${field.javaField + 'End'} != null">
                AND `${field.columnName}` BETWEEN ${field.javaField + 'Start'} AND ${field.javaField + 'End'}
            </if>
            <#break>
        <#case "LIKE">
            <#-- 模糊查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ''">
                AND `${field.columnName}` LIKE concat('%', #<#--防止#{}被解析-->{${field.javaField}}, '%')
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` LIKE concat('%', #<#--防止#{}被解析-->{${field.javaField}}, '%')
            </if>
        </#if>
        <#break>
        <#case "EQ">
        <#-- 等于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ''">
                AND `${field.columnName}` = #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` = #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        </#if>
            <#break>
        <#case "NEQ">
            <#-- 不等于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ''">
                AND `${field.columnName}` != #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` != #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        </#if>
            <#break>
        <#case "GT">
            <#-- 大于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ``">
                AND `${field.columnName}` > #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` > #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        </#if>
            <#break>
        <#case "GTE">
            <#-- 大于等于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ''">
                AND `${field.columnName}` <![CDATA[>=]]> #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
        </#if>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` <![CDATA[>=]]> #<#--防止#{}被解析-->{${field.javaField}}
            </if>
            <#break>
        <#case "LT">
        <#-- 小于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ``">
                AND `${field.columnName}` < #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` < #<#--防止#{}被解析-->{${field.javaField}}
            </if>
            <#break>
        </#if>
        <#case "LTE">
        <#-- 小于等于查询 -->
        <#if field.javaType== "String">
            <if test="${field.javaField} != null and ${field.javaField} != ``">
                AND `${field.columnName}` <![CDATA[<=]]> #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        <#else>
            <if test="${field.javaField} != null">
                AND `${field.columnName}` <![CDATA[<=]]> #<#--防止#{}被解析-->{${field.javaField}}
            </if>
        </#if>
        <#break>
    </#switch>
    </#if>
</#list>
        ORDER BY <#if hasSort == true>`sort`,</#if>`${pkColumn}` DESC
    </select>

    <#--详细信息-->
    <select id="select${entityClassName}ById" parameterType="Long" resultMap="${entityClassName}Result">
        <include refid="select${entityClassName}"/>
        WHERE `${pkColumn}` = #<#---->{id}
    </select>

    <!--逻辑删除-->
    <update id="remove${entityClassName}ByIds" parameterType="Long">
        UPDATE ${tableName}
        SET del_flag = 'T'
        WHERE ${pkColumn} IN
        <foreach collection="list" item="${pkName}" open="(" separator="," close=")">
            #<#---->{${pkName}}
        </foreach>
    </update>

    <!--物理删除-->
    <delete id="delete${entityClassName}ByIds" parameterType="Long">
        DELETE FROM ${tableName}  WHERE ${pkColumn} IN
        <foreach collection="list" item="${pkName}" open="(" separator="," close=")">
            #<#---->{${pkName}}
        </foreach>
    </delete>

    <!--新增-->
    <insert id="insert${entityClassName}" parameterType="${dtoPkg}.${dtoAddClassName}">
        INSERT INTO ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list columns as field>
            <if test="${field.javaField} != null<#if field.javaType=="String"> and ${field.javaField} != ''</#if>" >`${field.columnName}`<#if field_has_next>,</#if></if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list columns as field>
            <if test="${field.javaField} != null<#if field.javaType=="String"> and ${field.javaField} != ''</#if>" >#<#-- -->{${field.javaField}}<#if field_has_next>,</#if></if>
            </#list>
        </trim>
    </insert>


    <!--修改-->
    <update id="update${entityClassName}" parameterType="${dtoPkg}.${dtoUpdateClassName}">
        UPDATE ${tableName}
        <set>
            <#list columns as field>
            <if test="${field.javaField} != null<#if field.javaType=="String"> and ${field.javaField} != ''</#if>" >`${field.columnName}` = #<#---->{${field.javaField}}<#if field_has_next>,</#if></if>
            </#list>
        </set>
        WHERE ${pkColumn} = #<#---->{${pkName}}
    </update>

</mapper>
